// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'

// 路由配置
const routes = [
  // 登录页（无需登录权限）
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login/index.vue'),
    meta: { needAuth: false }
  },
  // 用例管理页（需登录）
  {
    path: '/case-manage',
    name: 'CaseManage',
    component: () => import('../views/CaseManage/index.vue'),
    meta: { needAuth: true }
  },
  // 日志页（需登录）
  {
    path: '/log',
    name: 'Log',
    component: () => import('../views/Log/index.vue'),
    meta: { needAuth: true }
  },
  // 报告中心（需登录）
  {
    path: '/report-center',
    name: 'ReportCenter',
    component: () => import('../views/ReportCenter/index.vue'),
    meta: { needAuth: true }
  },
  // 测试数据管理（需登录）
  {
    path: '/data-manage',
    name: 'DataManage',
    component: () => import('../views/DataManage/index.vue'),
    meta: { needAuth: true }
  },
  // 个人中心（需登录）
  {
    path: '/setting',
    name: 'Setting',
    component: () => import('../views/Setting/index.vue'),
    meta: { needAuth: true }
  },
  // 项目管理（需登录）
  {
    path: '/project-manage',
    name: 'ProjectManage',
    component: () => import('../views/ProjectManage/index.vue'),
    meta: { needAuth: true }
  },
  // 项目详情（需登录）
  {
    path: '/project-detail/:id',
    name: 'ProjectDetail',
    component: () => import('../views/ProjectDetail/index.vue'),
    meta: { needAuth: true }
  },
  // 默认跳转登录页
  {
    path: '/',
    redirect: '/login'
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound/index.vue')
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫：未登录用户跳转登录页
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  // 需要登录但未登录时，跳转登录页
  if (to.meta.needAuth && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router